package S02lianbiao;

public class T0106 {
    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public static void main(String[] args) {
        //添加
        int[] arr = {1, 2, 3, 4, 5, 6, 7};
        ListNode head = new ListNode(0);
        ListNode temp = head;
        for (int i = 0; i < arr.length; i++) {
            temp.next = new ListNode(arr[i]);
            temp = temp.next;
        }

        //执行
        head=getIntersectionNode(head,head);
        System.out.println(head.val);


        //打印
//        temp = head;
//        String str = new String("[");
//        while (temp != null) {
//            str = str + temp.val + ",";
//            temp = temp.next;
//        }
//        str = str + "]";
//        System.out.println(str);



    }

    public static ListNode  getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode a=headA;
        ListNode b=headB;
        while (a!=b){
            if(a==null){
                a=headB;
            }else {
                a=a.next;
            }
            if(b==null){
                b=headA;
            }else {
                b=b.next;
            }
        }
        return a;

    }
}
